package net.a6polo.a6poloweibo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import net.a6polo.a6poloweibo.sina.domain.Timeline;
import net.a6polo.a6poloweibo.sina.domain.WeiboItem;
import net.a6polo.a6poloweibo.util.AsynImageLoader;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SinaTimelineListViewAdapter extends BaseAdapter {

	private Timeline timeline;
	private Map<Integer, View> rowViews = new HashMap<Integer, View>();
	private Context context;

	public SinaTimelineListViewAdapter(Timeline timeline, Context context) {
		this.timeline = timeline;
		this.context = context;
	}
	
	public void addItem(Timeline timeline) {
		
		this.timeline.getStatuses().addAll(timeline.getStatuses());
	}

	@Override
	public int getCount() {
		if (null != timeline) {
			return timeline.getStatuses().size();
		} else {
			return 0;
		}

	}

	@Override
	public Object getItem(int position) {
		return timeline.getStatuses().get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View rowView = rowViews.get(position);
		
		
		
		if (null == rowView) {

			LayoutInflater layoutInflater = LayoutInflater.from(context);
			rowView = layoutInflater.inflate(
					R.layout.sina_timeline_listview_item, null);			
			
			WeiboItem item = (WeiboItem) getItem(position);
			
			TextView weiboId = (TextView) rowView.findViewById(R.id.sinaWeiboId);
			weiboId.setText(item.getId()+"");
			

			TextView userName = (TextView) rowView.findViewById(R.id.sinaWeiboUserName);
			TextView text = (TextView) rowView.findViewById(R.id.sinaWeiboText);			
			userName.setText(item.getUser().getName());
			text.setText(item.getText());
			

			TextView date = (TextView) rowView.findViewById(R.id.sinaWeiboDate);
			Date datetime = new Date(item.getCreated_at()); 
			SimpleDateFormat sdf=new SimpleDateFormat("MM-dd HH:mm");  
			String str = sdf.format(datetime);  
			date.setText(str);
			

			TextView from = (TextView) rowView.findViewById(R.id.txt_wb_item_from);
			from.setText(Html.fromHtml(item.getSource()) );
			from.setMovementMethod(LinkMovementMethod.getInstance());


			TextView redirect = (TextView) rowView.findViewById(R.id.txt_wb_item_redirect);
			TextView comment = (TextView) rowView.findViewById(R.id.txt_wb_item_comment);
			redirect.setText(item.getReposts_count()+"");
			comment.setText(item.getComments_count()+"");
			

			AsynImageLoader loader = new AsynImageLoader();
			ImageView userIcon = (ImageView)rowView.findViewById(R.id.img_wb_item_head);
			Bitmap userHead = loader.loadDrawableFromNet(userIcon,item.getUser().getProfile_image_url());
			userIcon.setImageBitmap(userHead);

			if(item.getUser().isVerified()) {
				ImageView vUser = (ImageView)rowView.findViewById(R.id.img_wb_item_V);
				vUser.setVisibility(ImageView.VISIBLE);
			}
			

			if(null != item.getThumbnail_pic()) {
				ImageView content_Pic = (ImageView)rowView.findViewById(R.id.img_wb_item_content_pic);
				content_Pic.setVisibility(ImageView.VISIBLE);
				Bitmap contentPic = loader.loadDrawableFromNet(content_Pic,item.getThumbnail_pic());
				content_Pic.setImageBitmap(contentPic);
			}
			

			if(null != item.getRetweeted_status()) {
				LinearLayout item_sublayout = (LinearLayout)rowView.findViewById(R.id.lyt_wb_item_sublayout);
				item_sublayout.setVisibility(LinearLayout.VISIBLE);
				TextView subcontent = (TextView) rowView.findViewById(R.id.txt_wb_item_subcontent);
				subcontent.setText(item.getRetweeted_status().getText());
				
				if(null != item.getRetweeted_status().getThumbnail_pic()) {
					ImageView sub_content_Pic = (ImageView)rowView.findViewById(R.id.img_wb_item_content_subpic);
					sub_content_Pic.setVisibility(ImageView.VISIBLE);
					Bitmap contentPic = loader.loadDrawableFromNet(sub_content_Pic,item.getRetweeted_status().getThumbnail_pic());
					sub_content_Pic.setImageBitmap(contentPic);
					
					
				}
			}
			

			rowViews.put(position, rowView);
			
			rowView.setOnClickListener(new OnClickListener()
	        {

	            @Override
	            public void onClick(View view)
	            {
	            	TextView weiboId = (TextView) view.findViewById(R.id.sinaWeiboId);
	            	Intent intent = new Intent();
	            	intent.putExtra("weiboId", weiboId.getText());
	    			//intent.setClass(context, WeiboDetailsActivity.class);
	    			intent.setClass(context, DetailTabActivity2.class);
	    			
	    			context.startActivity(intent);
	                
	            }
	            
	        });
		}
		
		

		return rowView;
	}

}
